<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="80%"
    top="5%"
  >
    <div style="height: 65vh;padding: 10px">
            <div style="display: flex;font-weight: bold;justify-content: center;align-items: center;font-size: 20px;padding: 10px">
              应急队伍人员推荐信息表
            </div>
      <t-form class="table-form" ref="form" :data="formData" labelWidth="100px" :rules="rules">
        <t-row>
          <t-col :span="10" style="display: flex;flex-direction: column;border: 0;padding: 0px !important">
            <t-row style="width: 100%;">
              <t-col :span="2" class="title"><span class="require">*</span>序号</t-col>
              <t-col :span="10" style="border-right: 0px">
                <t-form-item name="sort">
                  <t-input-number clearable v-model="formData.sort" :min="0" placeholder="请输入"></t-input-number>
                </t-form-item>
              </t-col>
            </t-row>
          </t-col>
          <t-col :span="2" style="border-left: 0;margin-left: -2px;">
          </t-col>
        </t-row>
<!--        <t-form-item name="sort" labelWidth="100px" label="序号">-->
<!--          序号: <t-input-number clearable v-model="formData.sort" :min="0" placeholder="请输入"></t-input-number>-->
<!--        </t-form-item>-->
        <t-row>
          <t-col :span="10" style="display: flex;flex-direction: column;border: 0;padding: 0px !important">
            <t-row style="width: 100%;">
              <t-col :span="2" class="title"><span class="require">*</span>姓名</t-col>
              <t-col :span="4">
               <t-form-item name="name">
                 <t-input clearable v-model="formData.name"></t-input>
               </t-form-item>
              </t-col>
              <t-col :span="2" class="title"><span class="require">*</span>性别</t-col>
              <t-col :span="4">
                <t-form-item name="sex">
                <t-radio-group v-model="formData.sex" :options="dict.type.sys_user_sex"></t-radio-group>
                </t-form-item>
              </t-col>
            </t-row>
            <t-row style="width: 100%">
              <t-col :span="2" class="title"><span class="require">*</span>民族</t-col>
              <t-col :span="4">
                <t-form-item name="national">
                <t-select v-model="formData.national" clearable placeholder="请选择民族" :options="dict.type.national">
                </t-select>
                </t-form-item>
              </t-col>
              <t-col :span="2" class="title"><span class="require">*</span>出生年月</t-col>
              <t-col :span="4">
                <t-form-item name="birthDate">
                <t-date-picker v-model="formData.birthDate"></t-date-picker>
                </t-form-item>
              </t-col>
            </t-row>
            <t-row style="width: 100%;">
              <t-col :span="2" class="title"><span class="require">*</span>学历</t-col>
              <t-col :span="4">
                <t-form-item name="education">
                <t-select v-model="formData.education" clearable placeholder="请选择学历" :options="dict.type.education">
                </t-select>
                </t-form-item>
              </t-col>
              <t-col :span="2" class="title"><span class="require">*</span>毕业院校</t-col>
              <t-col :span="4">
                <t-form-item name="school">
                <t-input clearable placeholder="请输入毕业院校" v-model="formData.school"></t-input>
                </t-form-item>
              </t-col>
            </t-row>
            <t-row style="width: 100%;">
              <t-col :span="2" class="title"><span class="require">*</span>技术职称</t-col>
              <t-col :span="4">
                <t-form-item name="profTitId">
                <t-tree-select clearable @change="onProfTitIdChange" filterable  :treeProps="{keys: {value: 'id',label:'name'}, valueMode: 'onlyLeaf'}"
                               v-model="formData.profTitId" :data="profTitOptions"
                               placeholder="请选择技术职称"></t-tree-select>
                </t-form-item>
              </t-col>
              <t-col :span="2" class="title"><span class="require">*</span>职称级别</t-col>
              <t-col :span="4">
                <t-form-item name="profTit">
                <t-select disabled v-model="formData.profTit" clearable placeholder="请选择职称级别" :options="dict.type.prof_tit">
                </t-select>
                </t-form-item>
              </t-col>
            </t-row>
          </t-col>
          <t-col :span="2" style="border-left: 0;margin-left: -2px;">
            <t-form-item style="display: flex;justify-content: center;align-items: center" name="sysAttMainVo.sysAttMainIds">
            <file-upload v-if="formData.sysAttMainVo.sysAttMainIds.length<=0" v-model="formData.sysAttMainVo.sysAttMainIds" accept="image/*" :fileList="fileLists"
                         placeholder="" tips="">
              <t-button variant="outline">
                <t-icon name="cloud-upload" slot="icon"/>
                <span class="require">*</span>
                上传头像
              </t-button>

            </file-upload>
            <div v-else>
              <file-upload ref="upload"
                v-model="formData.sysAttMainVo.sysAttMainIds" accept="image/*" :fileList="fileLists" placeholder="" tips="">
              <t-avatar style="cursor: pointer" shape="round" size="180px"  :image="fileLists[0].url"></t-avatar>
                <template #fileListDisplay><div></div></template>
              </file-upload>
            </div>
            </t-form-item>
          </t-col>
        </t-row>
        <t-row align="stretch">
          <t-col :span="12" style="border: 0;padding: 0px !important">
            <t-col style="height: 100%" :flex="3.9" class="title"><span class="require">*</span>所在单位</t-col>
            <t-col :flex="8">
              <t-form-item name="departmentId">
              <t-tree-select clearable filterable :treeProps="{keys: {value: 'id'}}" v-model="formData.departmentId"
                             :data="deptOptions" placeholder="选择单位"></t-tree-select>
              </t-form-item>
            </t-col>
            <t-col :flex="3.9" class="title">职务</t-col>
            <t-col :flex="13">
              <t-form-item name="duty">
              <t-input clearable v-model="formData.duty"></t-input>
              </t-form-item>
            </t-col>
          </t-col>

        </t-row>
        <t-row>
          <t-col :span="12" style="border: 0;padding: 0px !important">
            <t-col style="height: 100%" :flex="3.9" class="title"><span class="require">*</span>所属部门</t-col>
            <t-col :flex="8">
              <t-form-item name="section">
              <t-input clearable v-model="formData.section"></t-input>
              </t-form-item>
            </t-col>
            <t-col :flex="3.9" class="title"><span class="require">*</span>联系电话</t-col>
            <t-col :flex="13">
              <t-form-item name="phonenumber">
              <t-input clearable placeholder="请输入手机号" v-model="formData.phonenumber"></t-input>
              </t-form-item>
            </t-col>
          </t-col>


        </t-row>
        <t-row>
          <t-col :span="12" style="border: 0;padding: 0px !important;">
            <t-col :flex="4" class="title" style="min-height: 400px"><span class="require">*</span>个人介绍</t-col>
            <t-col :flex="26" style="height: 100%;min-height: 400px;">
              <t-form-item name="introduce">
              <tiny-mce-editor v-model="formData.introduce" :height="380"></tiny-mce-editor>
              </t-form-item>
            </t-col>
          </t-col>
        </t-row>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import TinyMceEditor from "@/components/TinyMceEditor/index.vue";
import FileUpload from "@/components/FileUpload/index.vue"
import { getFiles } from "@/api/common";

export default {
  name: "add-edit-team-member",
  components: {
    TinyMceEditor,
    FileUpload
  },
  dicts: ['sys_user_sex', 'national', 'education','prof_tit'],
  props: ['header', 'objId', 'parentId','departmentId'],
  data() {
    return {
      img: 'https://tdesign.gtimg.com/demo/demo-image-1.png',
      visible: true,
      loading: false,
      profTitOptions: [],
      deptOptions:[],
      fileLists: [],
      formData: {
        sort: 1,
        name: '',
        sex:'1',
        national: '',
        birthDate: '',
        education: '',
        duty: '',
        profTit: '',
        profTitId: null,
        school: '',
        departmentId: null,
        phonenumber: '',
        section: '',
        introduce: '',
        sysAttMainVo:{
          sysAttMainIds:[]
        }
      },
      rules: {
        name: [
          {
            required: true,
            message: "姓名不能为空",
          }
        ],
        sex: [
          {
            required: true,
            message: "性别不能为空",
          }
        ],
        national: [
          {
            required: true,
            message: "民族不能为空",
          }
        ],
        birthDate: [
          {
            required: true,
            message: "出生年月不能为空",
          }
        ],
        education: [
          {
            required: true,
            message: "学历不能为空",
          }
        ],
        school: [
          {
            required: true,
            message: "毕业院校不能为空",
          }
        ],
        profTitId: [
          {
            required: true,
            message: "技术职称不能为空",
          }
        ],
        departmentId: [
          {
            required: true,
            message: "所在单位不能为空",
          }
        ],
        section: [
          {
            required: true,
            message: "所属部门不能为空",
          }
        ],
        introduce: [
          {
            required: true,
            message: "个人介绍不能为空",
          }
        ],
        phonenumber: [
          {
            required: true,
            message: "联系电话不能为空",
          },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "联系电话请输入正确的手机号码",
          },
        ],
        'sysAttMainVo.sysAttMainIds':[
          {required: true, validator: (val) => val && val.length, message: "头像不能为空", type: "error"}
        ]
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
    departmentId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.formData.departmentId = val
        }
      }
    },
  },
  created() {
    this.getProfTitTreeSelect();
    this.$api.system.dept.treeSelectAllIn().then(response => {
      this.deptOptions = response.data;
    }).catch((e) => {
      this.$message.error(e.toString());
    });
  },
  methods: {
    getObj() {
      this.$api.wlaq.emergency.teamMemberGet(this.objId).then(response => {
        response.data.sysAttMainVo = response.data.sysAttMainVo || {sysAttMainIds:[]}
        Object.assign(this.formData, response.data)
        getFiles(this.objId).then((res) => {
          this.fileLists = []
          const ids = [];
          if (res.data && res.data.length) {
            res.data.forEach((item) => {
              ids.push(item.id);
              this.fileLists.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
            })
            this.formData.sysAttMainVo.sysAttMainIds = ids;
          }
        });
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId) {
            this.$api.wlaq.emergency.teamMemberUpdate(this.formData).then(() => {
              this.$message.success("修改成功");
              this.$emit("update:departmentId", this.formData.departmentId);
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.wlaq.emergency.teamMemberAdd(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("update:departmentId", this.formData.departmentId);
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        } else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
    onProfTitIdChange(val,content){
      this.formData.profTit =  content.node.data.level;
    },
    getProfTitTreeSelect() {
      return this.$api.system.profTit.highList().then(response => {
        this.profTitOptions = this.handleTree(response.data, "id");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    remove(){
      this.$refs.upload.removeFile()
    }
  }
}
</script>

<style lang="less" scoped>
.step-form {
  padding: 0 24px;
}

.t-row {
  margin-bottom: 14px;
}

.step-card {
  height: 100%;
  margin-bottom: 14px;
}

.table-form {
  /deep/ .t-radio.t-is-disabled .t-radio__label {
    color: unset
  }

  /deep/ .t-is-error .t-input__extra {
    position: static;
  }

  /deep/ .t-form__label {
    display: none;
  }

  .t-form__item {
    width: 100%;
  }

  /deep/ .t-textarea__inner {
    height: 100%;
  }

  .t-row {
    align-items: stretch;
    margin-bottom: 0px;
    //.t-form__item{
    //  width: 100%;
    //}
    .require {
      color: red;
      margin-right: 3px;
    }

    .t-col {
      border: 1px solid #ddd;
      color: #333;
      min-height: 50px;
      padding: 4px !important;
      //overflow: hidden;
      display: flex;
      align-items: center;
      margin: 0px -1px -1px 0px;
    }

    .title {
      background-color: #FAFAFA;
      justify-content: center;
      margin: 0px -1px -1px 0px;
      color: #606266;
      font-family: '宋体', serif;
      font-weight: bold;
      font-size: 14pt;
    }
  }
}
</style>
